package com.start_eternity.oasissafewalk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.start_eternity.oasissafewalk.mapper.StudentMapper;
import com.start_eternity.oasissafewalk.pojo.*;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.service.StudentService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private LoggService loggService;

    @Autowired
    private HttpServletRequest request;

    //   根据删除 id
    @Override
    public int delete(List<Integer> sId) {
        int rows = studentMapper.delete(sId);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据学生ID删除学生信息,删除的该学生的ID:" + sId);
        loggService.insert(logg);
        return rows;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //用logg记录
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());//记录执行的当前时间
        //获取请求头中的jwt令牌, 解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String) claims.get("username");
        logg.setLoggName(name);//记录执行者的名字
        logg.setLoggDescription("执行了查询学生的操作");//记录执行的具体操作
        loggService.insert(logg);//调用loggService接口将logg类的信息记录到数据库里面

        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<Student> students = studentMapper.queryPage(page, pageSize);
        Page<Student> p = (Page<Student>) students;
        //3.封装PageBean对象
        return new PageBean(p.getTotal(),p.getResult());
    }

    //根据id查询
    @Override
    public List<ParentStudent> selectById(Integer id) {
        List<ParentStudent> parentStudents = studentMapper.selectById(id);
        return parentStudents;
    }

    //插入数据
    @Override
    public int save(Student student) {
        int rows = studentMapper.insert(student);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行了插入学生的操作,该学生信息:" + student);
        loggService.insert(logg);
        return rows;
    }

    //修改数据
    @Override
    public int update(Student student) {
        int rows = studentMapper.update(student);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据ID修改学生信息,该学生信息:" + student);
        loggService.insert(logg);
        return rows;
    }

    /*@Override
    public void delete(List<Integer> ids) {
        enrollMapper.delete(ids);
    }*/
}
